package com.yuxi.Config;

import java.util.ArrayList;
import java.util.List;

import com.yuxi.Dao.InformationPortDao;
import com.yuxi.Dao.UserInfoDao;
import com.yuxi.Entity.InformationPort;
import com.yuxi.excelEntity.ComparatorPort;
import com.yuxi.spring.jbpm.Util.SpringContextUtils;

public class PortStaticUtil {
	public static List<InformationPort> portList=new ArrayList<InformationPort>();

	public static List<InformationPort> getPortList() {
		return portList;
	}

	public static void setPortList(List<InformationPort> portList) {
		PortStaticUtil.portList = portList;
	}
	
	public static InformationPort findById(Integer id) {		
		for (InformationPort informationPort : portList) {
			if(informationPort.getId()==id){
				return informationPort;
			}
		}
		return null;		
	}
	
	public static List<InformationPort> findAllPre(InformationPort port){		
		 List<InformationPort> rsList=new ArrayList<InformationPort>();
		 rsList.add(port);
		 findPre(port,rsList);
		 return rsList;		
	}
	
	public static List<InformationPort> findPre(InformationPort port,List<InformationPort> rsList){	
		
		while(port!=null&&port.getSuperId()!=0){
			if(port==null||port.getSuperId()==0){
				return null;
			}
			for (InformationPort informationPort : portList) {
				if(informationPort.getId()==port.getSuperId()){
					rsList.add(informationPort);
					port=informationPort;
				}
			}
		}
		return rsList;		
	}
	
	public static void  updatePorts(){
		InformationPortDao i=(InformationPortDao) SpringContextUtils.getBeanByClass(InformationPortDao.class); 		
		setPortList(i.getAllInfoPort());
		//System.out.println(portList);
	}
	
	public static  String getAllpreStr(Integer id){
		updatePorts();
		InformationPort port=findById(id);
		//System.out.println(port);
		List<InformationPort> ps=findAllPre(port);
		//System.out.println(ps);
		//ps.sort(new ComparatorPort());
		StringBuffer rs=new StringBuffer();
		Integer max=ps.size();
		Integer index=max;
		while(index!=0){
			if(index<max){
				rs.append("-");
			}
			rs.append(ps.get((index-1)).getPortName());
			index--;
		}
		return rs.toString();
	}
	
//	public static InformationPort findByLvel(InformationPort port,List<InformationPort> s){
//		List<InformationPort> rs=new ArrayList<InformationPort>();
//		while(port.getSuperId()!=0){
//			for (InformationPort informationPort : rs) {
//				informationPort.get
//			}
//		}
//	}
	
	
}
